﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers.world {
    import flash.events.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.models.world.*;

    public class FeedCommand extends Command {

        public var event:InfoEvent;
        public var world:WorldModel;
        public var userModel:UserModel;
        public var sunModel:SunModel;
        public var languageModel:LanguageModel;
        public var sessionModel:SessionModel;
        public var tickModel:TickModel;
        public var pendingModel:PendingModel;
        private var _pendingVO:PendingItemVO;

        override public function execute():void{
            var _local3:int;
            var _local4:Object;
            var _local5:Array;
            var _local6:TaskRequestEvent;
            var _local1:IEating = (this.world.getActor(this.event.id) as IEating);
            var _local2:int = this.pendingModel.getPendingItemCount(GameConstants.PENDING_COFFEE);
            l(("c eater.canEat: " + _local1.canEat));
            l(("isFull: " + this.isFull));
            if (((((_local1.canEat) && (((this.userModel.coffee - _local2) > 0)))) && (!(this.isFull)))){
                this.createPendingCoffeeVO();
                _local3 = ((_local1.hungryAt)<=this.tickModel.timestamp) ? this.tickModel.timestamp : _local1.hungryAt;
                _local3 = (_local3 + (this.sessionModel.doctorMealDuration - 10));
                l(("hungryAt: " + _local3));
                _local1.eat(_local3);
                this.addPendingCoffee();
                _local4 = {taskType:GameConstants.FEED_DOCTOR, itemType:GameConstants.KIOSK, onResult:this.onResult, onError:this.onError};
                _local5 = [new RequestVO("feed_doctor", {id:this.event.id})];
                _local6 = new TaskRequestEvent(EventConstants.SEND_TASK_REQUEST, _local5, _local4);
                dispatch(_local6);
                this.createRewardAnimation(_local1);
            } else {
                if (((_local1.canEat) && ((this.userModel.coffee <= 0)))){
                    log("sun_message_getcoffee");
                    dispatch(new InfoEvent(EventConstants.HIGHLIGHT_MACHINES, GameConstants.TYPE_KIOSK));
                    this.sunModel.simpleMessage = this.languageModel.getTextByKey("sun_message_getcoffee");
                    dispatch(new Event(EventConstants.HANDLE_NEXT_AVATAR_ACTION));
                } else {
                    dispatch(new Event(EventConstants.HANDLE_NEXT_AVATAR_ACTION));
                };
            };
        }
        private function createRewardAnimation(_arg1:IEating):void{
            var _local2:Object = new Object();
            _local2[EventConstants.ITEM_TYPE] = GameConstants.REWARD_COFFEE;
            _local2[EventConstants.TARGET_ID] = _arg1.id;
            dispatch(new InfoEvent(EventConstants.CREATE_REWARD_ANIMATION_INTO_HOSPITAL, null, _local2));
        }
        private function onResult(_arg1:ResultVO):void{
            var _local2:Object;
            var _local3:Object;
            var _local4:IEating;
            var _local5:IRoom;
            if (_arg1.success){
                _local2 = _arg1.data.feed_doctor;
                _local3 = _local2.pet;
                _local4 = (this.world.getActor(this.event.id) as IEating);
                _local5 = (this.world.getRoomById(_local4.roomId) as IRoom);
                dispatch(new InfoEvent(EventConstants.UPDATE_USER_DATA, null, _local2));
                dispatch(new InfoEvent(EventConstants.CREATE_REWARD_ICONS, _local5.id, _local2.items));
                this.removePendingCoffee();
                if (this.pendingCoffee <= 0){
                    _local4.hungryAt = _local3[ProtocolConstants.HUNGRY_AT];
                };
                _local5.machine.checkState();
            };
            dispatch(new Event(EventConstants.HANDLE_NEXT_AVATAR_ACTION));
        }
        private function createPendingCoffeeVO():void{
            this._pendingVO = new PendingItemVO(GameConstants.PENDING_COFFEE, 1);
        }
        private function addPendingCoffee():void{
            this.pendingModel.addPendingItem(this._pendingVO);
        }
        private function removePendingCoffee():void{
            this.pendingModel.removePendingItem(this._pendingVO);
        }
        private function get pendingCoffee():int{
            return (this.pendingModel.getPendingItemCount(GameConstants.PENDING_COFFEE));
        }
        private function onError(_arg1:String, _arg2:ResultVO):void{
            this.removePendingCoffee();
            dispatch(new Event(EventConstants.HANDLE_NEXT_AVATAR_ACTION));
            dispatch(new InfoEvent(EventConstants.ON_LOAD_ERROR, _arg1, _arg2.status));
        }
        private function get mealCount():int{
            var _local1:IEating = (this.world.getActor(this.event.id) as IEating);
            var _local2:int = (_local1.hungryAt - this.tickModel.timestamp);
            var _local3:Number = Math.ceil((_local2 / this.sessionModel.doctorMealDuration));
            return (_local3);
        }
        private function get isFull():Boolean{
            var _local1 = (this.mealCount >= GameConstants.DOCTOR_MEAL_COUNT);
            return (_local1);
        }

    }
}//package net.wooga.pets.controllers.world 
